package com.ttbj.core.aspect;

 
import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ttbj.core.annotation.DataSource;
import com.ttbj.core.dao.dataSource.DataBaseContextHolder;

/**
 * @Description: 自动数据源设定织入
 * 
 * @Author: Daniel
 * @CreateDate: 2015-6-3 下午03:00:47
 * @Version: V1.0
 * 
 * 
 */
@Aspect
@Component 
public class DataSourceAnnotationAspect {

	private static Logger logger = LoggerFactory.getLogger(DataSourceAnnotationAspect.class);

	public Object setDataSource(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		Signature signature = proceedingJoinPoint.getSignature();
		String methodName = signature.getName();
		Class<?> declaringType = signature.getDeclaringType();
		DataSource classAnnotationDataSource = (DataSource) declaringType.getAnnotation(DataSource.class);
		if (classAnnotationDataSource != null) {
			String dataSourceType = classAnnotationDataSource.value();
			DataBaseContextHolder.setDatabaseType(dataSourceType);
			logger.debug("setDataSource class - {}.{}()数据源切换为:{}", declaringType, methodName, dataSourceType);
			Object[] args = proceedingJoinPoint.getArgs();
			Object result = proceedingJoinPoint.proceed(args);
			DataBaseContextHolder.setDatabaseType(DataBaseContextHolder.DEFAULT_TARGET_DATASOURCE);
			return result;
		}

		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		DataSource methodAnnotationDataSource = (DataSource) method.getAnnotation(DataSource.class);
		if (methodAnnotationDataSource != null) {
			String dataSourceType = methodAnnotationDataSource.value();
			DataBaseContextHolder.setDatabaseType(dataSourceType);
			logger.debug("setDataSource method - {}.{}()数据源切换为:{}", declaringType, methodName, dataSourceType);
			Object[] args = proceedingJoinPoint.getArgs();
			Object result = proceedingJoinPoint.proceed(args);
			DataBaseContextHolder.setDatabaseType(DataBaseContextHolder.DEFAULT_TARGET_DATASOURCE);
			return result;
		}
		DataBaseContextHolder.setDatabaseType(DataBaseContextHolder.DEFAULT_TARGET_DATASOURCE);
		logger.debug("setDataSource default - {}.{}()数据源默认为:{}", declaringType, methodName, DataBaseContextHolder.DEFAULT_TARGET_DATASOURCE);
		Object[] args = proceedingJoinPoint.getArgs();
		Object result = proceedingJoinPoint.proceed(args);
		return result;
	}

	public void clearDataSource(JoinPoint joinPoint) throws Throwable {
		String currentDatabaseType = DataBaseContextHolder.getDatabaseType();

		Signature signature = joinPoint.getSignature();
		String methodName = signature.getName();
		Class<?> declaringType = signature.getDeclaringType();
		DataSource classAnnotationDataSource = (DataSource) declaringType.getAnnotation(DataSource.class);
		if (classAnnotationDataSource != null) {
			String originDataSourceType = classAnnotationDataSource.value();
			logger.debug("clearDataSource class - {}.{}()原配置{}，清除当前数据源{}", declaringType, methodName, originDataSourceType, currentDatabaseType);
			DataBaseContextHolder.cleanDatabaseType();
			return;
		}

		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		DataSource methodAnnotationDataSource = (DataSource) method.getAnnotation(DataSource.class);
		if (methodAnnotationDataSource != null) {
			String originDataSourceType = methodAnnotationDataSource.value();
			logger.debug("clearDataSource class - {}.{}()原配置{}，清除当前数据源{}", declaringType, methodName, originDataSourceType, currentDatabaseType);
			DataBaseContextHolder.cleanDatabaseType();
			return;
		}

		logger.debug("setDataSource default - {}.{}()原配置{}，清除默认数据源{}", declaringType, methodName, DataBaseContextHolder.DEFAULT_TARGET_DATASOURCE, currentDatabaseType);
		DataBaseContextHolder.cleanDatabaseType();
		return;
	}
}
